/*
 *  Copyright (c) 2010 Stephen Nelson <stephen@sfnelson.org>
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */

package org.sfnelson.bma.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Visibility;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class ErrorConsole extends Composite {

	private static ErrorConsoleUiBinder uiBinder = GWT
			.create(ErrorConsoleUiBinder.class);

	interface ErrorConsoleUiBinder extends UiBinder<Widget, ErrorConsole> {
	}

	@UiField Label message;

	private static ErrorConsole console;

	public ErrorConsole() {
		initWidget(uiBinder.createAndBindUi(this));

		hide();

		console = this;
	}

	private void setMessage(String message) {
		this.message.setText(message);
		this.getStyleElement().getStyle().setVisibility(Visibility.VISIBLE);
		timer.schedule(5000);
	}

	private void hide() {
		this.getStyleElement().getStyle().setVisibility(Visibility.HIDDEN);
	}

	public static void addMessage(String message, Throwable ex) {
		console.setMessage(message);
	}

	private Timer timer = new Timer() {
		@Override
		public void run() {
			hide();
		}
	};
}
